<?php
defined('BASEPATH') or exit ('No direct script access allowed');

class Helper extends MY_Before_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->datas ['page_title'] = "团队管理";
    }

    public function index()
    {
        check_privilege(230);   //权限检测
        $this->datas ['trash_count'] = $this->myModel->get_trash_count();
        $this->query_list();
    }

    //跳转到详情页
    public function info($id)
    {
        check_privilege(231);   //权限检测
        $this->load->model('permission/role_model');
        $this->datas['role_list']    = $this->role_model->get_role_list();
        $info                        = $this->myModel->get_info($id);
        $this->datas['gallery_list'] = $this->myModel->get_gallery($info['hospital_id']);//医院下的小屋相册
        $this->datas['form_action']  = 'update';
        $this->datas['page_title']   = '<span style="color: #428bca;">[' . $info["real_name"] . ']</span>' . "人员详情";
        //获取小屋经理所能负责的模块
        $this->datas['helper_mdel_list'] = $this->myModel->get_serve_item();
        $this->datas['info']             = $info;
        $this->load->view($this->dwt_info, $this->datas);
    }

    /**
     * 分页数据
     *
     * @param number $page
     */
    private function query_list($page = 1, $filter = array(), $load_page = TRUE)
    {
        $get = $this->input->get();
        if (!empty ($get)) {
            $filter = array_merge($filter, $get);
        }
        if (isset ($filter ['page']) && $filter ['page'] > 0) {
            $page = $filter ['page'];
        }

        $item_list             = $this->myModel->get_list($filter, $page);
        $this->datas ['lists'] = $item_list ['list'];
        $this->datas ['pager'] = $item_list ['pager'];
        if ($this->datas ['full_page']) {
            $this->load->view($this->dwt_list, $this->datas);
        } else {
            return $this->load->view($this->dwt_list, $this->datas, TRUE);
        }
    }

    /**
     * 接收ajax请求
     * @param unknown $action
     * @param number $item_id
     */
    public function ajax($action, $item_id = 0)
    {
        $datas            = $this->input->get();
        $datas['item_id'] = $item_id;
        $this->process_ajax($action, $datas);
    }

    /**
     * 接收表单post数据
     */
    public function post()
    {
        $post = $this->input->post();
        $this->process_ajax($post['action'], $post);
    }

    /**
     * 处理各种ajax请求
     * @param unknown $action
     * @param unknown $datas
     * @return json
     */
    private function process_ajax($action, $datas = array())
    {
        $content        = '';
        $message        = '';
        $append_data    = array();
        $exclude_for_id = array('add', 'insert', 'update', 'query');
        if ($action == 'update') {
            $datas['item_id'] = $datas['id'];
        }
        if (!in_array($action, $exclude_for_id) && (!isset($datas['item_id']) || empty($datas['item_id']))) {
            make_json_error("未指明操作对象！[305:$action]");
        }
        if (isset($datas['job'])) {
            if ($datas['job'] == 0) {
                $datas['role_id'] = 0;//普通员工角色ID为0
            } else {
                $datas['module_list'] = '';//小屋经理由角色控制模块 所以不需要模块功能列表
            }
        }
        $result['error'] = 0;
        switch ($action) {
            case 'query':
                check_privilege(230);   //权限检测
                $this->datas ['full_page'] = FALSE;
                $content                   = $this->query_list();
                //统计数量
                $append_data['trash_count'] = $this->myModel->get_trash_count();
                break;
            case 'edit':
                check_privilege(232);   //权限检测
                $this->datas['modal_title'] = $action == 'add' ? '添加团队人员' : '编辑团队人员';
                $this->datas['form_action'] = $action == 'add' ? 'insert' : 'update';
                $this->datas['full_page']   = FALSE;
                $info                       = array();
                if ($action == 'edit') {
                    $info = $this->myModel->get_info($datas['item_id']);
                } else {
                    $info['province_id'] = 0;
                    $info['city_id']     = 0;
                    $info['area_id']     = 0;
                }
                $append_data['formData'] = $info;
                $this->datas['info']     = $info;
                break;
            case 'update':
                check_privilege(232);   //权限检测
                $datas['module_list'] = isset($datas['module_list']) ? $datas['module_list'] : array();
                $result               = $this->myModel->edit_data($datas, $datas['item_id'], 'update', 'fast_edit');
                if ($result['error'] == 1) {
                    make_json_error($result['message']);
                } else {
                    $message                 = '修改成功';
                    $append_data['followUp'] = 'redirect';
                    $append_data['redirect'] = site_url($this->datas['my_router']);
                }
                break;
            case 'drop_img':
                check_privilege(232);   //权限检测
                $result = $this->myModel->drop_image($datas['item_id']);
                break;
            case 'respwd':
                check_privilege(231);   //权限检测
                $datas['password']       = md5(strtolower(123456));
                $result                  = $this->myModel->edit_data($datas, $datas['item_id']);
                $append_data['followUp'] = 'queryList';
                break;
            case 'hide':
            case 'show':
                check_privilege(232);   //权限检测
                if ($datas['item_id'] == $this->session->userdata('helper_id')) {
                    make_json_error('您属于非法操作！');
                } else {
                    $update_datas['active'] = $action == 'hide' ? 0 : 1;
                    $result                 = $this->myModel->edit_data($update_datas, $datas['item_id']);
                }
                break;
            case 'fast_edit':
                check_privilege(232);   //权限检测
                $field = trim($datas['act']);
                $val   = trim(urldecode($datas['val']));
                if ($this->myModel->check_single_field($field)) {
                    $update_datas[$field] = $val;
                    $result               = $this->myModel->edit_data($update_datas, $datas['item_id'], FALSE, 'fast_edit');
                    $content              = $val;
                } else {
                    $result['error']   = 1;
                    $result['message'] = "未授权操作！";
                }
                break;
            default:
                $result['error']   = 300;
                $result['message'] = "非法操作！[" . $datas['item_id'] . "]";
                break;
        }
        if ($result['error'] > 0) {
            make_json_error($result['message']);
        } else {
            make_json_result($content, $message, $append_data);
        }
    }

    /**
     * 接收表格批量数据
     */
    public function batch()
    {
        check_privilege(232);   //权限检测
        //判定编辑和删除权限
        $post            = $this->input->post();
        $content         = '';
        $message         = '';
        $append_data     = array();
        $result['error'] = 0;
        $action          = $post['action'];
        $items_id        = !empty($post['checkboxes']) ? $post['checkboxes'] : '';
        switch ($action) {
            case 'show':
            case 'hide':
                $update_datas['active'] = $action == 'hide' ? 0 : 1;
                //批量更新
                if (in_array($this->session->userdata('helper_id'), $items_id)) {
                    make_json_error('您属于非法操作！');
                } else {
                    $result = $this->myModel->edit_data($update_datas, $items_id, TRUE);
                }
                break;
            default:
                $result['error']   = 300;
                $result['message'] = "非法操作！[" . $action . "]";
                break;
        }
        $append_data['followUp'] = 'queryList';
        if ($result['error'] > 0) {
            make_json_error($result['message'] . "\r\n[Err:" . $result['error'] . "]");
        } else {
            make_json_result($content, $message, $append_data);
        }
    }
}
